//
//  NewShopCell.m
//  CarUser
//
//  Created by 王森林 on 2019/1/18.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "NewShopCell.h"
#import "ShopInfoOtherViewController.h"
#import "HomeDetailView.h"
@interface  NewShopCell()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *carAry;
@property (nonatomic, strong) HomeDetailView *homeDetailView;
@end

#define BTNTAG 1000
@implementation NewShopCell

- (void)handleBtnAction:(id)sender
{
    
    UIButton *btn = sender;
    NSInteger tag = btn.tag - BTNTAG;
    NSDictionary *dict  = self.carAry[tag];
    
    ShopInfoOtherViewController *controller = [[ShopInfoOtherViewController alloc] init];
    
    controller.TRADERS_ID = [dict objectForKey:@"TRADERS_ID"];
    
    
    
    [[self viewController].navigationController pushViewController:controller animated:YES];
}

-(void)createUIWithData
{
    [self.contentView  removeAllSubviews];
    self.bgView = nil;
    self.homeDetailView = nil;
    self.scrollView = nil;
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.homeDetailView];
    [self.bgView addSubview:self.scrollView];
    [self createConstraint];
    
    
    float width = (BOUNDWIDTH - 40)/3;
    float height = 90;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(15+(width+10)*(self.carAry.count), self.scrollView.size.height);
    [self.carAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSDictionary *dict  = self.carAry[idx];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame =  CGRectMake(15+(width+10)*idx, 0, width, height);
        btn.tag = BTNTAG + idx;
        [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height - 30)];
//        [imageView sd_setImageWithURL:[NSURL URLWithString:APPEND_URL(BASE_URL, [dict objectForKey:@"ZST000"])]];
//        imageView.image = IMG([dict objectForKey:@"imageName"]);
        [imageView sd_setImageWithURL:[NSURL URLWithString:APPEND_URL(BASE_URL, [dict objectForKey:@"MDTP00"])]];
        [btn addSubview:imageView];
        
        
        
        
        NSString *str = [dict objectForKey:@"NAME00"];
        UIFont *font = FONT_SIZE(12);
        CGRect nameRect =[str boundingRectWithSize:CGSizeMake(btn.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil] context:nil];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.frame.size.height+5,btn.frame.size.width , nameRect.size.height+5)];
        titleLabel.text =  str;
        titleLabel.textColor = RGB(51, 51, 51);
        titleLabel.font = FONT_SIZE(13);
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.numberOfLines = 2;
        [btn addSubview:titleLabel];

        [self.scrollView addSubview:btn];
        
        
    }];
    
}

- (void)createConstraint
{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(0);
        (void)make.left.and.right;
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    
    [self.homeDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(0);
        (void)make.left.and.right;
        make.height.mas_equalTo(48);
    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.homeDetailView.mas_bottom).offset(0);
        (void)make.left.and.right;
        make.bottom.mas_equalTo(self.bgView.mas_bottom).offset(0);
    }];
    
}

- (void)setData:(id)data
{
    if (data){
//        self.carAry =  @[@{@"imageName":@"car_test4",@"title":@"东莞市浩宏汽车销售有限公司 "},
//                                            @{@"imageName":@"car_test4",@"title":@"武汉临空港国际贸易有限公司"},
//                                                @{@"imageName":@"car_test4",@"title":@"武汉的经销商"},
//                                                @{@"imageName":@"car_test4",@"title":@"测试经销商"}
//
//                                                ];
        self.carAry = [NSArray arrayWithArray:data];
        [self createUIWithData];
    }
}


-  (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
    
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
    }
    return _scrollView;
    
}

- (HomeDetailView *)homeDetailView
{
    if (!_homeDetailView) {
        _homeDetailView = [[HomeDetailView alloc] initWithType:HOME_DETAIL_TYPE_SHOP withBlock:^(id  _Nonnull response) {
        
        }];
    }
    return _homeDetailView;
}

@end
